// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   KTCAController.java

package com.kmh.musicplay.util;

import java.util.HashMap;

import com.kt.ca.CAAdaptor;
import com.kt.ca.CAEventListener;
import com.kt.ca.CAException;

// Referenced classes of package com.kt.icod.epg.core:
//            CAController

public class KTCAController implements CAEventListener
{

//	private static final byte TAG_SYSTEM_INFO_REQUEST = 0;
//	private static final byte TAG_SYSTEM_INFO_RESPONSE = 1;
//	private static final byte TAG_CHANNEL_STATUS_RESPONSE = 3;
//	private static final byte TAG_PIN_QUERY = 10;
//	private static final byte TAG_PIN_RESPONSE = 11;
//	private static final byte TAG_CHANGE_AGE_LIMIT_REQUEST = 12;
//	private static final byte TAG_CHANGE_AGE_LIMIT_RESPONSE = 13;
//	private static final byte TAG_PIN_CHANGE_REQUEST = 14;
//	private static final byte TAG_PIN_CHANGE_RESPONSE = 15;
//	private static final byte TAG_PARENTAL_RATING_LOCK_REQUEST = 16;
//	private static final byte TAG_PARENTAL_RATING_LOCK_RESPONSE = 17;
//	private static final byte TAG_OSD_NOTIFY = 24;
//	private static final byte TAG_ALTI_CAS_STATUS_NOTIFY = 25;
//	private static final byte TAG_PRODUCT_INFO_REQUEST = 36;
//	private static final byte TAG_PRODUCT_INFO_RESPONSE = 37;
//	private static final int TRANSACION_ID = 12289;
//	private static final int TRANSACTION_ID_NOTIFY = -1;
//	private static final int CHANNEL_NOT_SCRAMBLED = 1;
//	private static final int CHANNEL_CAN_DESCRAMBLE = 2;
//	private static final int CHANNEL_BLACK_OUT = 4;
//	private static final int CHANNEL_SPOT_ENABLE = 8;
//	private static final int CHANNEL_NOT_SUBSCRIBED = 16;
//	private static final int CHANNEL_PURCHASED_VOD = 32;
//	private static final int CHANNEL_NOT_PURCHASED_VOD = 64;
//	private static final int CHANNEL_PURCHASED_PPV = 2048;
//	private static final int CHANNEL_NOT_PURCHASED_PPV = 4096;
//	private static final int CHANNEL_BONUS = 8192;
//	private static final int CHANNEL_PARENTAL_RATING_LIMIT = 16384;
//	private static final int RESPONSE_OK = 0;
//	private static final int RESPONSE_UNKNOWN_ERROR = 1;
//	private static final int RESPONSE_TIME_OUT = 2;
//	private static final int RESPONSE_NOT_INITIALIZED = 3;
//	private static final int RESPONSE_NOT_EXIST = 4;
//	private static final int RESPONSE_SERVER_NOT_FOUND = 5;
//	private static final int RESPONSE_INVALID_PARAMETER = 6;
//	private static final int RESPONSE_MISSING_PARAMETER = 7;
//	private static final int RESPONSE_UNKNOWN_ID = 8;
//	private static final int RESPONSE_PURCHASE_DENIED = 9;
//	private static final int RESPONSE_NO_CREDIT = 10;
//	private static final int RESPONSE_CANCEL_IS_TOO_LATE = 11;
//	private static final int RESPONSE_INVALID_PIN = 12;
//	private static final int RESPONSE_INVALID_USER_ID = 13;
//	private static final int VOD_ERROR = 12288;
//	private static final int CAS_NOT_INITIALIZED = 0;
//	private static final int CAS_IS_PAIRING = 1;
//	private static final int CAS_NORMAL = 2;
//	private static final int CAS_SUSPENDED = 3;
//	private static final int CAS_EXPIRED = 4;
//	private static final int CAS_ERROR = 5;
//	private static final int PRODUCT_ALACARTE = 2;
//	private static final int PRODUCT_PPS = 3;
//	private static final int PRODUCT_OPPV = 4;
//	private static final int PRODUCT_IPPV = 5;
//	private static final int PRODUCT_PPD = 6;
//	private static final int PRODUCT_ISU = 7;
//	private static final int PRODUCT_VOD = 16;
//	private static final byte ATTR_MASTER_PIN = 53;
//	private static final byte ATTR_ORG_PIN = 51;
//	private static final byte ATTR_NEW_PIN = 52;
//	private static final byte ATTR_USER_ID = 55;
//	private static final byte ATTR_PARENTAL_RATING_LOCK_STATUS = 54;
//	private static final byte ATTR_PROCESSING_STATUS = 0;
//	private static final byte ATTR_CAS_STATUS = 3;
//	private static final byte ATTR_STRING = 43;
//	private static final byte ATTR_AGE_LIMIT = 24;
//	private static final byte ATTR_PROGRAM_STRUCTURE = 42;
//	private static final byte ATTR_CHANNEL_STATUS = 1;
//	private static final byte ATTR_PRODUCT_INFO_DATA = 62;
//	private static final byte ATTR_CHANNEL_ID = 15;
//	private static final short PRODUCT_INFO_B2B = 8192;
//	private static final short PRODUCT_INFO_FULL_PACKAGE = 4096;
	
	private long appID;
	private static final Object WAIT_FOR_RESPONSE = new Object();
	private byte responseMessage[];
	private byte requestTag;
	
	public KTCAController()
	{
		responseMessage = null;
		appID = CAAdaptor.getInstance().addCAEventListener(this);
	}

	public void dispose()
	{
		CAAdaptor.getInstance().removeCAEventListener(this);
	}

	//	public boolean releasePRLock(long pin)
	//	{
	//		return requestPRLock(pin, (byte) 1);
	//	}
	//
	//	public boolean resetPRLock()
	//	{
	//		return requestPRLock(0L, (byte) 0);
	//	}
	//
	//	private boolean requestPRLock(long pin, byte lockStatus)
	//	{
	//		byte requestMessage[] = new byte[17];
	//		byte responseMessage[] = null;
	//		boolean succeed = false;
	//		int index = 0;
	//		requestMessage[index++] = 0;
	//		requestMessage[index++] = 15;
	//		requestMessage[index++] = 54;
	//		requestMessage[index++] = 0;
	//		requestMessage[index++] = 1;
	//		requestMessage[index++] = lockStatus;
	//		requestMessage[index++] = 51;
	//		requestMessage[index++] = 0;
	//		requestMessage[index++] = 8;
	//		StringBuffer password = new StringBuffer();
	//		password.append(pin);
	//		if (password.length() < 4)
	//		{
	//			for (int i = password.length(); i < 4; i++)
	//				password.insert(0, 0);
	//
	//		}
	//		byte orgPin[] = Utils.convertToByte(password.toString(), 8);
	//		int i = 0;
	//		for (int n = orgPin.length; i < n; i++)
	//			requestMessage[index++] = orgPin[i];
	//
	//		responseMessage = request(12289L, (byte) 16, requestMessage);
	//		if (responseMessage != null)
	//		{
	//			HashMap attrMap = getAttrMap(responseMessage);
	//			Byte data[] = (Byte[]) (Byte[]) attrMap.get(getAttributeName((byte) 0));
	//			if (data != null && Utils.convertToShort(data) == 0)
	//				succeed = true;
	//		}
	//		return succeed;
	//	}

	public boolean checkPassword(String password)
	{
		byte requestMessage[] = new byte[17];
		byte responseMessage[] = null;
		boolean succeed = false;
		int index = 0;
		requestMessage[index++] = 0;
		requestMessage[index++] = 15;
		requestMessage[index++] = 55;
		requestMessage[index++] = 0;
		requestMessage[index++] = 1;
		requestMessage[index++] = 0;
		requestMessage[index++] = 51;
		requestMessage[index++] = 0;
		requestMessage[index++] = 8;
		byte orgPin[] = convertToByte(password, 8);
		int i = 0;
		for (int n = orgPin.length; i < n; i++)
			requestMessage[index++] = orgPin[i];

		responseMessage = request(20608L, (byte) 10, requestMessage);
		if (responseMessage != null)
		{
			HashMap attrMap = getAttrMap(responseMessage);
			Byte data[] = (Byte[]) (Byte[]) attrMap.get(getAttributeName((byte) 0));
			if (data != null && convertToShort(data) == 0)
				succeed = true;
		}
		return succeed;
	}

	//	public boolean changePassword(String oldPassword, String newPassword)
	//	{
	//		byte requestMessage[] = new byte[39];
	//		byte responseMessage[] = null;
	//		byte pin[] = null;
	//		boolean succeed = false;
	//		int index = 0;
	//		requestMessage[index++] = 0;
	//		requestMessage[index++] = 37;
	//		requestMessage[index++] = 55;
	//		requestMessage[index++] = 0;
	//		requestMessage[index++] = 1;
	//		requestMessage[index++] = 0;
	//		requestMessage[index++] = 53;
	//		requestMessage[index++] = 0;
	//		requestMessage[index++] = 8;
	//		pin = Utils.convertToByte(oldPassword, 8);
	//		int i = 0;
	//		for (int n = pin.length; i < n; i++)
	//			requestMessage[index++] = pin[i];
	//
	//		requestMessage[index++] = 51;
	//		requestMessage[index++] = 0;
	//		requestMessage[index++] = 8;
	//		pin = Utils.convertToByte(oldPassword, 8);
	//		i = 0;
	//		for (int n = pin.length; i < n; i++)
	//			requestMessage[index++] = pin[i];
	//
	//		requestMessage[index++] = 52;
	//		requestMessage[index++] = 0;
	//		requestMessage[index++] = 8;
	//		pin = Utils.convertToByte(newPassword, 8);
	//		i = 0;
	//		for (int n = pin.length; i < n; i++)
	//			requestMessage[index++] = pin[i];
	//
	//		responseMessage = request(12289L, (byte) 14, requestMessage);
	//		if (responseMessage != null)
	//		{
	//			HashMap attrMap = getAttrMap(responseMessage);
	//			Byte data[] = (Byte[]) (Byte[]) attrMap.get(getAttributeName((byte) 0));
	//			if (data != null && Utils.convertToShort(data) == 0)
	//				succeed = true;
	//		}
	//		return succeed;
	//	}
	//
	//	public boolean resetAgeLimit(String password)
	//	{
	//		return setAgeLimit(19, password);
	//	}
	//
	//	public boolean setAgeLimit(int age, String password)
	//	{
	//		byte requestMessage[] = new byte[17];
	//		byte responseMessage[] = null;
	//		byte pin[] = null;
	//		boolean succeed = false;
	//		int index = 0;
	//		requestMessage[index++] = 0;
	//		requestMessage[index++] = 15;
	//		requestMessage[index++] = 24;
	//		requestMessage[index++] = 0;
	//		requestMessage[index++] = 1;
	//		requestMessage[index++] = (byte) (age & 0xff);
	//		requestMessage[index++] = 51;
	//		requestMessage[index++] = 0;
	//		requestMessage[index++] = 8;
	//		pin = Utils.convertToByte(password, 8);
	//		int i = 0;
	//		for (int n = pin.length; i < n; i++)
	//			requestMessage[index++] = pin[i];
	//
	//		responseMessage = request(12289L, (byte) 12, requestMessage);
	//		if (responseMessage != null)
	//		{
	//			HashMap attrMap = getAttrMap(responseMessage);
	//			Byte data[] = (Byte[]) (Byte[]) attrMap.get(getAttributeName((byte) 0));
	//			if (data != null && Utils.convertToShort(data) == 0)
	//				succeed = true;
	//		}
	//		return succeed;
	//	}
	//
	//	public short checkCAStatus()
	//	{
	//		byte requestMessage[] = new byte[2];
	//		byte responseMessage[] = null;
	//		requestMessage[0] = 0;
	//		requestMessage[1] = 0;
	//		responseMessage = request(12289L, (byte) 0, requestMessage);
	//		if (responseMessage != null)
	//		{
	//			HashMap attrMap = getAttrMap(responseMessage);
	//			Byte data[] = null;
	//			data = (Byte[]) (Byte[]) attrMap.get(getAttributeName((byte) 0));
	//			if (data != null && Utils.convertToShort(data) != 0)
	//				return 305;
	//			data = (Byte[]) (Byte[]) attrMap.get(getAttributeName((byte) 3));
	//			if (data != null)
	//			{
	//				short status = Utils.convertToShort(data);
	//				switch (status)
	//				{
	//					case 5: // '\005'
	//						return 305;
	//
	//					case 4: // '\004'
	//						return 304;
	//
	//					case 1: // '\001'
	//						return 301;
	//
	//					case 0: // '\0'
	//						return 300;
	//
	//					case 3: // '\003'
	//						return 303;
	//				}
	//			}
	//		}
	//		return 202;
	//	}
	//
	//	public int isB2bSubscriber()
	//	{
	//		byte requestMessage[] = new byte[2];
	//		byte responseMessage[] = null;
	//		requestMessage[0] = 0;
	//		requestMessage[1] = 0;
	//		responseMessage = request(12289L, (byte) 36, requestMessage);
	//		if (responseMessage != null)
	//		{
	//			HashMap attrMap = getAttrMap(responseMessage);
	//			Byte data[] = null;
	//			data = (Byte[]) (Byte[]) attrMap.get(getAttributeName((byte) 0));
	//			if (data != null)
	//			{
	//				if (Utils.convertToShort(data) != 0)
	//					return -1;
	//			}
	//			else
	//			{
	//				return -1;
	//			}
	//			data = (Byte[]) (Byte[]) attrMap.get(getAttributeName((byte) 62));
	//			if (data != null)
	//			{
	//				short productInfo = Utils.convertToShort(data);
	//				if ((productInfo & 0x2000) != 0)
	//					return 0;
	//			}
	//		}
	//		return 1;
	//	}
	//
	//	public int isFullPackage()
	//	{
	//		byte requestMessage[] = new byte[2];
	//		byte responseMessage[] = null;
	//		requestMessage[0] = 0;
	//		requestMessage[1] = 0;
	//		responseMessage = request(12289L, (byte) 36, requestMessage);
	//		if (responseMessage != null)
	//		{
	//			HashMap attrMap = getAttrMap(responseMessage);
	//			Byte data[] = null;
	//			data = (Byte[]) (Byte[]) attrMap.get(getAttributeName((byte) 0));
	//			if (data != null)
	//			{
	//				if (Utils.convertToShort(data) != 0)
	//					return -1;
	//			}
	//			else
	//			{
	//				return -1;
	//			}
	//			data = (Byte[]) (Byte[]) attrMap.get(getAttributeName((byte) 62));
	//			if (data != null)
	//			{
	//				short productInfo = Utils.convertToShort(data);
	//				if ((productInfo & 0x1000) != 0)
	//					return 0;
	//			}
	//		}
	//		return 1;
	//	}
	//
	//	public int isVodOnly()
	//	{
	//		return 1;
	//	}

	private synchronized byte[] request(long transId, byte tag, byte requestMessage[])
	{
		synchronized (WAIT_FOR_RESPONSE)
		{
			try
			{
				requestTag = tag;
				responseMessage = null;
				CAAdaptor.getInstance().request(appID, transId, tag, requestMessage);
				try
				{
					WAIT_FOR_RESPONSE.wait();
				}
				catch (InterruptedException e)
				{
					e.printStackTrace();
				}
			}
			catch (CAException e)
			{
				e.printStackTrace();
			}
		}
		return responseMessage;
	}

	public void caEventArrived(long transId, int tag, byte message[])
	{
		if (transId == 2L)
		{
			responseMessage = null;
			switch (tag)
			{
				case 1: // '\001'
					if (requestTag == 0)
						responseMessage = message;
					break;

				case 11: // '\013'
					if (requestTag == 10)
						responseMessage = message;
					break;

				case 13: // '\r'
					if (requestTag == 12)
						responseMessage = message;
					break;

				case 15: // '\017'
					if (requestTag == 14)
						responseMessage = message;
					break;

				case 17: // '\021'
					if (requestTag == 16)
						responseMessage = message;
					break;

				case 37: // '%'
					if (requestTag == 36)
						responseMessage = message;
					break;
			}
			synchronized (WAIT_FOR_RESPONSE)
			{
				WAIT_FOR_RESPONSE.notifyAll();
			}
		}
		else if (transId == -1L)
		{
			//            HashMap attrMap = getAttrMap(message);
			//            Byte data[] = null;
			//            switch(tag)
			//            {
			//            default:
			//                break;
			//
			//            case 3: // '\003'
			//                data = (Byte[])(Byte[])attrMap.get(getAttributeName((byte)15));
			//                if(data == null)
			//                    return;
			//                int serviceId = Utils.convertToInt(data);
			//                if(!isValidChannelEvent(serviceId))
			//                    return;
			//                data = (Byte[])(Byte[])attrMap.get(getAttributeName((byte)1));
			//                if(data != null)
			//                    switch(Utils.convertToInt(data))
			//                    {
			//                    case 16: // '\020'
			//                        fireCAEvent((short)200, serviceId);
			//                        break;
			//
			//                    case 16384: 
			//                        fireCAEvent((short)201, serviceId);
			//                        break;
			//
			//                    case 1: // '\001'
			//                    case 2: // '\002'
			//                        fireCAEvent((short)202, serviceId);
			//                        break;
			//                    }
			//                break;
			//
			//            case 25: // '\031'
			//                data = (Byte[])(Byte[])attrMap.get(getAttributeName((byte)0));
			//                if(data != null)
			//                {
			//                    int processingStatus = Utils.convertToShort(data);
			//                    if(processingStatus != 0)
			//                    {
			//                        if((processingStatus & 0x3000) == 12288)
			//                            fireCAEvent((short)307, Integer.toHexString(processingStatus).toUpperCase());
			//                        else
			//                            fireCAEvent((short)306, Integer.toHexString(processingStatus).toUpperCase());
			//                        return;
			//                    }
			//                } else
			//                {
			//                    return;
			//                }
			//                data = (Byte[])(Byte[])attrMap.get(getAttributeName((byte)3));
			//                if(data == null)
			//                    break;
			//                switch(Utils.convertToShort(data))
			//                {
			//                case 0: // '\0'
			//                    fireCAEvent((short)300);
			//                    break;
			//
			//                case 1: // '\001'
			//                    fireCAEvent((short)301);
			//                    break;
			//
			//                case 2: // '\002'
			//                    fireCAEvent((short)302);
			//                    break;
			//
			//                case 3: // '\003'
			//                    fireCAEvent((short)303);
			//                    break;
			//
			//                case 4: // '\004'
			//                    fireCAEvent((short)304);
			//                    break;
			//
			//                case 5: // '\005'
			//                    fireCAEvent((short)305);
			//                    break;
			//                }
			//                break;
			//            }
		}
	}

	private HashMap getAttrMap(byte message[])
	{
		HashMap map = new HashMap();
		int dataLength = 0;
		int index = 0;
		dataLength = message[index] >= 0 ? ((int) (message[index])) : message[index] + 256;
		index++;
		dataLength <<= 8;
		dataLength += message[index++];
		String attributeName = null;
		Byte data[] = null;
		while (index - 1 < dataLength)
		{
			attributeName = getAttributeName(message[index++]);
			int attrLength = 0;
			attrLength = message[index] >= 0 ? ((int) (message[index])) : message[index] + 256;
			index++;
			attrLength <<= 8;
			attrLength += message[index++];
			data = new Byte[attrLength];
			for (int j = 0; j < attrLength; j++)
				data[j] = new Byte(message[index++]);

			map.put(attributeName, data);
		}
		return map;
	}

	private String getAttributeName(byte attribute)
	{
		StringBuffer attributeName = new StringBuffer();
		switch (attribute)
		{
			case 53: // '5'
				attributeName.append("MASTER PIN");
				break;

			case 51: // '3'
				attributeName.append("ORG PIN");
				break;

			case 52: // '4'
				attributeName.append("NEW PIN");
				break;

			case 55: // '7'
				attributeName.append("USER ID");
				break;

			case 54: // '6'
				attributeName.append("PARENTAL RATING LOCK STATUS");
				break;

			case 0: // '\0'
				attributeName.append("PROCESSING STATUS");
				break;

			case 3: // '\003'
				attributeName.append("CAS STATUS");
				break;

			case 43: // '+'
				attributeName.append("STRING");
				break;

			case 24: // '\030'
				attributeName.append("AGE LIMIT");
				break;

			case 42: // '*'
				attributeName.append("PROGRAM STRUCTURE");
				break;

			case 1: // '\001'
				attributeName.append("CHANNEL STATUS");
				break;

			case 62: // '>'
				attributeName.append("PRODUCT INFO DATA");
				break;

			case 15: // '\017'
				attributeName.append("CHANNEL ID");
				break;
		}
		return attributeName.toString();
	}

//	private String getTagName(int tag)
//	{
//		StringBuffer tagName = new StringBuffer();
//		switch (tag)
//		{
//			case 3: // '\003'
//				tagName.append("CHANNEL STATUS RESPONSE");
//				break;
//
//			case 10: // '\n'
//				tagName.append("PIN QUERY");
//				break;
//
//			case 11: // '\013'
//				tagName.append("PIN RESPONSE");
//				break;
//
//			case 12: // '\f'
//				tagName.append("CHANGE AGE LIMIT REQUEST");
//				break;
//
//			case 13: // '\r'
//				tagName.append("CHANGE AGE LIMIT RESPONSE");
//				break;
//
//			case 14: // '\016'
//				tagName.append("PIN CHANGE REQUEST");
//				break;
//
//			case 15: // '\017'
//				tagName.append("PIN CHANGE RESPONSE");
//				break;
//
//			case 16: // '\020'
//				tagName.append("PARENTAL RATING LOCK REQUEST");
//				break;
//
//			case 17: // '\021'
//				tagName.append("PARENTAL RATING LOCK RESPONSE");
//				break;
//
//			case 24: // '\030'
//				tagName.append("OSD NOTIFY");
//				break;
//
//			case 25: // '\031'
//				tagName.append("ALTI CAS STATUS NOFIFY");
//				break;
//
//			case 36: // '$'
//				tagName.append("PRODUCT INFO REQUEST");
//				break;
//
//			case 37: // '%'
//				tagName.append("PRODUCT INFO RESPONSE");
//				break;
//
//			case 0: // '\0'
//				tagName.append("SYSTEM INFO REQUEST");
//				break;
//
//			case 1: // '\001'
//				tagName.append("SYSTEM INFO RESPONSE");
//				break;
//		}
//		return tagName.toString();
//	}

//	private boolean isValidChannelEvent(int serviceId)
//	{
//		boolean flag = false;
//		Channel ch = ChannelController.getInstance().getCurrentChannel();
//		DvbLocator locator = null;
//		if (ch != null && ch.getSIService() != null)
//			locator = ch.getSIService().getDvbLocator();
//		if (locator != null)
//		{
//			int currentServiceId = locator.getServiceId();
//			if (currentServiceId == serviceId)
//				flag = true;
//		}
//		return flag;
//	}

	public static short convertToShort(Byte value[])
	{
		if (value == null || value.length != 2)
			throw new IllegalArgumentException();
		short convertedValue = 0;
		for (int i = 0; i < 2; i++)
		{
			convertedValue += value[i].byteValue() >= 0 ? ((short) (value[i].byteValue())) : ((short) (value[i].byteValue() + 256));
			if (i < 1)
				convertedValue <<= 8;
		}

		return convertedValue;
	}

	public static byte[] convertToByte(String value, int byteSize)
	{
		if (value == null || value.length() > byteSize)
			throw new IllegalArgumentException();
		byte convertedValue[] = new byte[byteSize];
		int index = 0;
		if (value.length() < byteSize)
			index = byteSize - value.length();
		int i = 0;
		for (int n = value.length(); i < n && i <= byteSize; i++)
			convertedValue[index + i] = (byte) value.charAt(i);

		return convertedValue;
	}

	public boolean isVGSBox()
	{
		return false;
	}

	public boolean isChannelSubscribable()
	{
		return false;
	}
}
